<?php
class TeamStats {
    var $won = 0;
    var $drawn = 0;
    var $lost = 0;

    var $tdFor = 0;
    var $tdAg = 0;
    var $casFor = 0;
    var $casAg = 0;

    var $highTdDiff;
    var $lowTdDiff;
    var $highTdScore;
    var $lowTdScore;
    var $highTdFor;
    var $lowTdFor;
    var $highOppoTdFor;
    var $lowOppoTdFor;

    var $highCasDiff;
    var $lowCasDiff;
    var $highCasScore;
    var $lowCasScore;
    var $highCasFor;
    var $lowCasFor;
    var $highOppoCasFor;
    var $lowOppoCasFor;

    var $wins = 0;
    var $noLoss = 0;
    var $noWin = 0;
    var $loss = 0;

    var $currentWins = 0;
    var $currentNoLoss = 0;
    var $currentNoWin = 0;
    var $currentLoss = 0;

    var $played = 0;

    function TeamStats($team = null) {
        $this->highTdDiff = new HighLowGame(0);
        $this->lowTdDiff = new HighLowGame(1);
        $this->highTdScore = new HighLowGame(2);
        $this->lowTdScore = new HighLowGame(3);
        $this->highTdFor = new HighLowGame(4);
        $this->lowTdFor = new HighLowGame(5);
        $this->highOppoTdFor = new HighLowGame(4);
        $this->lowOppoTdFor = new HighLowGame(5);

        $this->highCasDiff = new HighLowGame(0);
        $this->lowCasDiff = new HighLowGame(1);
        $this->highCasScore = new HighLowGame(2);
        $this->lowCasScore = new HighLowGame(3);
        $this->highCasFor = new HighLowGame(4);
        $this->lowCasFor = new HighLowGame(5);
        $this->highOppoCasFor = new HighLowGame(4);
        $this->lowOppoCasFor = new HighLowGame(5);
        if (isset ($team)) {
            $this->getTeamValues($team);
        }
    }

    function getRound($value, $div) {
        return round($value / ($div == 0 ? 1 : $div), 2);
    }

    /**
     * Initialises the stats with all the games for the team
     */
    function getTeamValues(& $team) {
        $rows = array ();
        $rs = executeQuery("select m.home_record, m.away_record, m.match_id from matches m " .
        		"join match_records h on h.match_id=m.match_id and h.team_id=$team->id " .
        		"union " .
        		"select m.home_record, m.away_record, m.match_id from matches m " .
        		"join match_records a on a.match_id=m.match_id and a.team_id=$team->id " .
        		"order by match_id");
        $mrd = new MatchRecordDao();
        while ($row = mysql_fetch_row($rs)) {
            $homeRecord = $mrd->load($row[0]);
            $awayRecord = $mrd->load($row[1]);
            if ($homeRecord->teamId == $team->id) {
                $this->addMatch($homeRecord, $awayRecord, $row[2]);
            } else {
				$this->addMatch($awayRecord, $homeRecord, $row[2]);
			}
        }
    }

    function addMatch($myRecord, $opponentRecord, $matchId) {
        $this->tdFor += $myRecord->score;
        $this->tdAg += $opponentRecord->score;
        $this->casFor += $myRecord->casualties;
        $this->casAg += $opponentRecord->casualties;
        $this->played++;

        $this->highTdScore->updateValue($myRecord->score, $opponentRecord->score, $matchId);
        $this->lowTdScore->updateValue($myRecord->score, $opponentRecord->score, $matchId);
        $this->highTdDiff->updateValue($myRecord->score, $opponentRecord->score, $matchId);
        $this->lowTdDiff->updateValue($myRecord->score, $opponentRecord->score, $matchId);
        $this->highTdFor->updateValue($myRecord->score, $opponentRecord->score, $matchId);
        $this->lowTdFor->updateValue($myRecord->score, $opponentRecord->score, $matchId);
        $this->highOppoTdFor->updateValue($opponentRecord->score, $myRecord->score, $matchId);
        $this->lowOppoTdFor->updateValue($opponentRecord->score, $myRecord->score, $matchId);

        $this->highCasScore->updateValue($myRecord->casualties, $opponentRecord->casualties, $matchId);
        $this->lowCasScore->updateValue($myRecord->casualties, $opponentRecord->casualties, $matchId);
        $this->highCasDiff->updateValue($myRecord->casualties, $opponentRecord->casualties, $matchId);
        $this->lowCasDiff->updateValue($myRecord->casualties, $opponentRecord->casualties, $matchId);
        $this->highCasFor->updateValue($myRecord->casualties, $opponentRecord->casualties, $matchId);
        $this->lowCasFor->updateValue($myRecord->casualties, $opponentRecord->casualties, $matchId);
        $this->highOppoCasFor->updateValue($opponentRecord->casualties, $myRecord->casualties, $matchId);
        $this->lowOppoCasFor->updateValue($opponentRecord->casualties, $myRecord->casualties, $matchId);

        if ($myRecord->score > $opponentRecord->score) {
            $this->currentWins++;
            $this->currentNoLoss++;
            $this->currentNoWin = 0;
            $this->currentLoss = 0;
            $this->won++;
        } else
            if ($myRecord->score == $opponentRecord->score) {
                $this->currentWins = 0;
                $this->currentNoLoss++;
                $this->currentNoWin++;
                $this->currentLoss = 0;
                $this->drawn++;
            } else {
                $this->currentWins = 0;
                $this->currentNoLoss = 0;
                $this->currentNoWin++;
                $this->currentLoss++;
                $this->lost++;
            }

        $this->updateStreaks();
    }

    function updateStreaks() {
        if ($this->currentWins > $this->wins) {
            $this->wins = $this->currentWins;
        }

        if ($this->currentNoLoss > $this->noLoss) {
            $this->noLoss = $this->currentNoLoss;
        }

        if ($this->currentNoWin > $this->noWin) {
            $this->noWin = $this->currentNoWin;
        }

        if ($this->currentLoss > $this->loss) {
            $this->loss = $this->currentLoss;
        }
    }

    function getNetTDs() {
        return $this->tdFor - $this->tdAg;
    }

    function getNetCas() {
        return $this->casFor - $this->casAg;
    }

    function getAverageNetTD() {
        return $this->played > 0 ? round($this->getNetTDs() / $this->played, 2) : 0;
    }

    function getAverageNetCas() {
        return $this->played > 0 ? round($this->getNetCas() / $this->played, 2) : 0;
    }

    function getAverageScore() {
        return $this->played > 0 ? round($this->tdFor / $this->played, 2) : 0;
    }

    function getAverageConceded() {
        return $this->played > 0 ? round($this->tdAg / $this->played, 2) : 0;
    }

    function getAverageInflicted() {
        return $this->played > 0 ? round($this->casFor / $this->played, 2) : 0;
    }

    function getAverageSuffered() {
        return $this->played > 0 ? round($this->casAg / $this->played, 2) : 0;
    }

}
?>